package com.smart.city.logger;

import java.util.List;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;

public class AccSensor implements Sensable {
	private Float[] value;
	private SensorManager sensorManager = null;
	private Sensor accSensor = null;
	private AccSensor instance;
	
	public AccSensor(Context context) {
		this.instance = this;
		this.value = new Float[] {0f, 0f, 0f};
		this.sensorManager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
		
		List<Sensor> sensorList = this.sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER);
		if (sensorList.size() != 0) {
			this.accSensor = sensorList.get(0);
			
			this.sensorManager.registerListener(new SensorEventListener() {

				public void onAccuracyChanged(Sensor sensor, int accuracy) {}

				public void onSensorChanged(SensorEvent event) {
					synchronized (instance) {
						instance.value = new Float[3];
						instance.value[0] = event.values[0];
						instance.value[1] = event.values[1];
						instance.value[2] = event.values[2];
					}
				}
			},accSensor, SensorManager.SENSOR_DELAY_GAME);
			
		}
	}
	

	public String getFormatedString() {
		Float[] tempSensorValue = getCurrentValue();
		String returnString = String.format("/%.6f/%.6f/%.6f",
				tempSensorValue[0], tempSensorValue[1], tempSensorValue[2]);
		return returnString;
	}


	public Float[] getValue() {
		return getCurrentValue();
	}
	
	synchronized private Float[] getCurrentValue() {
		return this.value.clone();
	}
}
